package com.woniu.work.Service;

import com.woniu.common.utils.ResponseData;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @Author hht
 * @Date Create by 2024/11/26 10:14
 */
@Service
public class HhtProjectService {

    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;

    /**
     * 部署流程，获取流程定义的id
     * @Author hht
     * @Date Create by 2024/11/26 14:11
     */
    public ResponseData<?> deployFlow(String bpmnFile) {
        Deployment projectDeployment = repositoryService.createDeployment()
                .addClasspathResource("processes/project/" + bpmnFile)
                .name("项目流程")
                .deploy();
        return new ResponseData<>().ok(projectDeployment.getId());
    }

    /**
     * 通过流程定义的id获得该流程的ProcessDefinition对象
     * 通过ProcessDefinition对象的id启动流程实例
     * @Author hht
     * @Date Create by 2024/11/26 14:12
     */
    public ResponseData<?> startProcess(String deploymentId) {
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deploymentId)
                .singleResult();
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());
        return new ResponseData<>().ok(processInstance.getProcessInstanceId());
    }

    public void completeTask(String processInstanceId, Integer approvalResult, String roleName) {
        Task task = taskService.createTaskQuery()
                .processInstanceId(processInstanceId)
                .singleResult();
        if(approvalResult == 1) {
            if("总监".equals(roleName)) {
                taskService.complete(task.getId());
            } else {
                //经理审批通过
                HashMap<String, Object> map = new HashMap<>();
                map.put("approvalResult", approvalResult);
                System.out.println(map);
                taskService.complete(task.getId(), map);
            }
        } else {
            //判断是否为总监审批，是就直接删除流程实例，不是就回退到总监审批节点
            if("总监".equals(roleName)) {
                runtimeService.deleteProcessInstance(processInstanceId, "审批不通过");
            } else {
                HashMap<String, Object> map = new HashMap<>();
                map.put("approvalResult", approvalResult);
                System.out.println(map);
                taskService.complete(task.getId(), map);
            }
        }


    }

    /**
     * 删除整个流程实例
     * @Author hht
     * @Date Create by 2024/11/27 10:31
     */
    public void deleteProcess(String processInstanceId) {
        runtimeService.deleteProcessInstance(processInstanceId, "删除流程实例");
    }
}
